/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ItemProdutoGUI.java
 *
 * Created on 23/07/2011, 20:54:51
 */

package carrancao.gui;

import carrancao.controlador.Fachada;
import carrancao.entidades.Fornecedor;
import carrancao.entidades.ItemProduto;
import carrancao.exception.CadastrarItemProdutoException;
import carrancao.util.ValidaData;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Lubnnia
 */
public class ItemProdutoGUI extends javax.swing.JFrame {

    /** Creates new form ItemProdutoGUI */
    public ItemProdutoGUI() {
        initComponents();
        setLocationRelativeTo(null);
        
        jFormattedTextFieldDataCadastro.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelNomeItem = new javax.swing.JLabel();
        jTextFieldItem = new javax.swing.JTextField();
        jLabelDataCadastro = new javax.swing.JLabel();
        jFormattedTextFieldDataCadastro = new javax.swing.JFormattedTextField();
        jLabelQtd = new javax.swing.JLabel();
        jTextFieldQtd = new javax.swing.JTextField();
        jLabelValorUnitario = new javax.swing.JLabel();
        jFormattedTextFieldValorUnitario = new javax.swing.JFormattedTextField();
        jLabel1 = new javax.swing.JLabel();
        jFormattedTextFieldValorTotal = new javax.swing.JFormattedTextField();
        jLabelObs = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaObs = new javax.swing.JTextArea();
        jButtonSalvar = new javax.swing.JButton();
        jButtonFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Cadastro de Item de Produto");

        jLabelNomeItem.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabelNomeItem.setText("Nome do Iem:");

        jTextFieldItem.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabelDataCadastro.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabelDataCadastro.setText("Data de Cadastro:");

        jFormattedTextFieldDataCadastro.setEditable(false);
        try {
            jFormattedTextFieldDataCadastro.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextFieldDataCadastro.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        jFormattedTextFieldDataCadastro.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabelQtd.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabelQtd.setText("Qtd do Produto:");

        jTextFieldQtd.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabelValorUnitario.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabelValorUnitario.setText("Valor Uni.:");

        try {
            jFormattedTextFieldValorUnitario.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextFieldValorUnitario.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        jFormattedTextFieldValorUnitario.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabel1.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel1.setText("Valor Total:");

        try {
            jFormattedTextFieldValorTotal.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }
        jFormattedTextFieldValorTotal.setFocusLostBehavior(javax.swing.JFormattedTextField.PERSIST);
        jFormattedTextFieldValorTotal.setFont(new java.awt.Font("Tahoma", 0, 12));

        jLabelObs.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabelObs.setText("Observações Gerais:");

        jTextAreaObs.setColumns(20);
        jTextAreaObs.setRows(5);
        jScrollPane1.setViewportView(jTextAreaObs);

        jButtonSalvar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonSalvar.setText("Salvar");
        jButtonSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonSalvarActionPerformed(evt);
            }
        });

        jButtonFechar.setFont(new java.awt.Font("Tahoma", 0, 14));
        jButtonFechar.setText("Fechar");
        jButtonFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabelQtd)
                .addGap(18, 18, 18)
                .addComponent(jLabelValorUnitario)
                .addGap(18, 18, 18)
                .addComponent(jLabel1))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jTextFieldQtd, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jFormattedTextFieldValorUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jFormattedTextFieldValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jLabelObs))
            .addGroup(layout.createSequentialGroup()
                .addGap(122, 122, 122)
                .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addComponent(jLabelNomeItem))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextFieldItem, javax.swing.GroupLayout.PREFERRED_SIZE, 381, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelDataCadastro)
                        .addGap(29, 29, 29))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jFormattedTextFieldDataCadastro)
                        .addContainerGap())))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 517, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNomeItem)
                    .addComponent(jLabelDataCadastro))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldItem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldDataCadastro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabelQtd)
                    .addComponent(jLabelValorUnitario)
                    .addComponent(jLabel1))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextFieldQtd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldValorUnitario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextFieldValorTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(11, 11, 11)
                .addComponent(jLabelObs)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonFechar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(35, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void limparCampos() {
        jTextFieldItem.setText("");
        jTextFieldQtd.setText("");
        jTextAreaObs.setText("");
        jFormattedTextFieldValorUnitario.setText("");
        jFormattedTextFieldValorTotal.setText("");
    }

    private void jButtonFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonFecharActionPerformed
       dispose();
    }//GEN-LAST:event_jButtonFecharActionPerformed

    private void jButtonSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonSalvarActionPerformed
        try {
            if(jTextFieldItem.getText().equals("") || jTextFieldQtd.getText().equals("")) {
                JOptionPane.showMessageDialog(null, "Caro usuário favor preencher todos os campos obrigatórios!",
                            "Mensagem do Sistema", JOptionPane.INFORMATION_MESSAGE);
            } else {

                Fornecedor fornecedor = null;

                String itemAux = jTextFieldItem.getText().toUpperCase();
                int qtdAux = Integer.parseInt(jTextFieldQtd.getText());
                String obsAux = jTextAreaObs.getText().toUpperCase();
                Double valorUnitarioAux = Double.parseDouble(jFormattedTextFieldValorUnitario.getText());
                Double valorTotalAux = Double.parseDouble(jFormattedTextFieldValorTotal.getText());

                SimpleDateFormat formatoData = new SimpleDateFormat("dd/MM/yyyy");
                String dataCadastroAux = jFormattedTextFieldDataCadastro.getText();
                Date dataCadastro = formatoData.parse(dataCadastroAux);

                ValidaData DATE = new ValidaData();
		dataCadastroAux = DATE.formatData(dataCadastroAux);


                //ItemProduto item = new ItemProduto(fornecedor, obsAux, obsAux, qtdAux, valorUnitarioAux, valorTotalAux, dataCadastro);
                //fornecedor = (Fornecedor) jComboBoxFornecedor.getSelectedItem();

                //item.setFornecedor(fornecedor);

                ItemProduto item = new ItemProduto(itemAux, obsAux, qtdAux, valorUnitarioAux, valorTotalAux, dataCadastro);
                Fachada.getInstance().getItem_produtoControl().cadastrarItemProduto(item);

                limparCampos();

            }

        } catch (ParseException ex) {
            Logger.getLogger(ClienteGUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (CadastrarItemProdutoException ex) {
            Logger.getLogger(ItemProdutoGUI.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jButtonSalvarActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ItemProdutoGUI().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonFechar;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JFormattedTextField jFormattedTextFieldDataCadastro;
    private javax.swing.JFormattedTextField jFormattedTextFieldValorTotal;
    private javax.swing.JFormattedTextField jFormattedTextFieldValorUnitario;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabelDataCadastro;
    private javax.swing.JLabel jLabelNomeItem;
    private javax.swing.JLabel jLabelObs;
    private javax.swing.JLabel jLabelQtd;
    private javax.swing.JLabel jLabelValorUnitario;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaObs;
    private javax.swing.JTextField jTextFieldItem;
    private javax.swing.JTextField jTextFieldQtd;
    // End of variables declaration//GEN-END:variables

}
